package com.cnxjs.shs4et.service.impl;

import com.cnxjs.shs4et.common.Result;
import com.cnxjs.shs4et.entity.Discussion;
import com.cnxjs.shs4et.repository.DiscussionRepository;
import com.cnxjs.shs4et.service.DiscussionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DiscussionImpl implements DiscussionService {

    @Autowired
    private DiscussionRepository discussionRepository;

    // 点赞
    @Override
    public void addLike(int likeId) {
        Discussion discussion = discussionRepository.findById(likeId);
        discussion.setDiscussionComment(discussion.getDiscussionComment() + 1);
        discussionRepository.save(discussion);
    }

    @Override
    public Result findAll(int comment) {
        return Result.success(discussionRepository.findAllByDiscussionCommentOrderByPublishTimeDesc(comment));
    }

    @Override
    public Result add(Discussion discussion) {
        return Result.success(discussionRepository.save(discussion));
    }

    @Override
    public Result delete(int id) {
        Discussion discussion = discussionRepository.findById(id);
        discussion.setIsLocked(1);
        discussionRepository.save(discussion);
        return Result.success("删除成功");
    }
}
